package com.stud.framework.config;

import com.stud.common.utils.StringUtils;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: sdx
 * @Date: 2022/12/11 21:32
 **/
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis")
@Data
public class RedissonConfig {

    private String host;

    private int port;

    private int database;

    private String password;


    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);

        if (StringUtils.isNotEmpty(password)) {
            config.useSingleServer().setPassword(password);
        }

        return Redisson.create(config);
    }
}
